package com.module.organization;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.apache.poi.poifs.storage.ListManagedBlock;

import base.util.Page;
import base.util.ResolveUnit;
import com.activityClient.client.ActivityThread;
import com.module.organization.manager.CreditManager;
import com.module.organization.manager.OrganizationManager;
import com.module.organization.vo.Credit;
import com.module.organization.vo.Creditimg;
import com.module.organization.vo.Organization;
import com.module.organization.vo.OrganizationBaseBean;
import com.module.organization.vo.OrganizationConstant;
import com.module.sys.LoginSession;
import com.webClient.client.SocketClient;
import com.webClient.service.WebClientService;

public class CreditService {
	CreditManager creditManager = CreditManager.getInstance();
	OrganizationManager organizationManager = OrganizationManager.getInstance();
	static WebClientService ss = null;
	static SocketClient sClient = null;
	static Logger logger = Logger.getLogger(CreditService.class.getName());
	
	/**
	 * 查询子组织  ym
	 * @param cr
	 * @param start
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	public Page queryAllList(Credit cr, final Integer start, final Integer limit)
			throws Exception {
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		Organization orgent = new Organization();
		orgent.setId(LoginSession.getOrgid());
		cr.setOrgId(organizationManager.getChildereNodeId(orgent));
		CreditManager creditManager = CreditManager.getInstance();
		Page resultPage = creditManager.list(page, cr);
		return resultPage;

	}
	
//	public List<Organization> listName(String orgType){
//		
//		try {
//			return  creditManager.getNameList(orgType,LoginSession.getOrgid());
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//		}
//		return null;
//	}
	
	/**
	 * 根据组织id查所属组织
	 */
	public List<Organization> listName(){
		try{
			return creditManager.getNameList(LoginSession.getOrgid());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 修改企业信息
	 * @param credit
	 * @return
	 * @throws Exception
	 */
	public Map upt(Credit credit)throws Exception{
		CreditManager creditManager = CreditManager.getInstance();
		boolean isTrue = false;
		 if(credit.getId()!=null){
	        	isTrue=creditManager.update(credit);
	        }
		 Map<String, Object> statusMap = new HashMap<String, Object>();
			statusMap.put("state", isTrue);
		 if (isTrue == true) {
			 	creditManager.save(credit);
				statusMap.put("msg", "修改成功");
			} else {
				statusMap.put("msg", "修改失败");
			}
		return statusMap;
	}
	


	public Credit queryById(Long id) throws Exception {
		CreditManager cm = CreditManager.getInstance();
		Credit ce = cm.queryById(id);
		return ce;
	}

	public Credit queryByName(String name) throws Exception {
		CreditManager cm = CreditManager.getInstance();
		Credit credit = new Credit();
		credit.setOrgName(name);
		Credit ce = cm.queryByName(name);
		return credit;
	}
	
	/**
	 * 修改或添加
	 * @param credit
	 * @param imgList
	 * @return
	 */

	
	public Map<String, Object> saveCredit(Credit credit) throws Exception{ 
		credit.setCreaterid(LoginSession.getUserid()); 
		credit.setCreatetime(new Date()); 
		CreditManager cm = CreditManager.getInstance(); 
		List<Creditimg> crimg = credit.getItems(); 
		Map<String, Object> statusMap = new HashMap<String, Object>(); 
		String isTrue=null;
		//boolean isTrue1 = false;
		if(credit.getId()!=null){ 
		isTrue=cm.updateOrsave(credit); 
		statusMap.put("state", isTrue); 
			if (isTrue == "1") { 
			statusMap.put("msg", "组织名称已存在，请重新输入"); 
			} else if(isTrue == "2"){ 
			statusMap.put("msg", "保存成功"); 
			}else if(isTrue == "4"){ 
				statusMap.put("msg", "修改成功！"); 
			}else if(isTrue == "5"){ 
				statusMap.put("msg", "修改失败！"); 
			}else{
				statusMap.put("msg", "保存失败");
			}
		} 

		return statusMap; 
		}
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean remove(Long id) throws Exception{
		Credit credit = new Credit();
		credit.setId(id);
		HashMap statusMap = new HashMap();
		CreditManager creditManager = CreditManager.getInstance();
		boolean isTrue = creditManager.del(credit);
		if (isTrue == true) {
			statusMap.put("msg", "删除成功");
		} else {
			statusMap.put("msg", "删除失败");
		}
		return isTrue;
	}
	

	private OrganizationBaseBean returnValue(Map map) throws Exception {
		try {
			String sendmsg = getJson(map);
			ActivityThread tc = ActivityThread.getInstance();
			logger.info("org发送的报文是：" + sendmsg);
			String receiveMessage = null;
 			receiveMessage = tc.sendAndReceive(sendmsg, map.get("sid")
					.toString());
			logger.info("org接收的报文是：" + receiveMessage);
			OrganizationBaseBean beanUnit = ResolveUnit.resolve(receiveMessage,
					OrganizationBaseBean.class);
			return beanUnit;
		} catch (Exception e) {
			throw e;
		}
	}

	private String getJson(Map map) throws Exception {
		String sessionId = UUID.randomUUID().toString();
		map.put(OrganizationConstant.STATUS_NAME,
				OrganizationConstant.STATUS_SEND);
		map.put(OrganizationConstant.SESSIONID_NAME, sessionId);
		map.put(OrganizationConstant.B_ID,
				OrganizationConstant.ORGANIZATION_BID);
		map.put(OrganizationConstant.CN, "CreditManager");
		String json = ResolveUnit.getJsonStr(map);
		return json;
	}
	


}
